<?php
/**
 *基础控制器
 *
 * @category     Shuxun Cms
 * @date         2015年9月21日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         Controller.php
 */
namespace  Scshux\Mvc;
use Phalcon\Mvc\Controller;
use Scshux\Utils\JSON;
class ControllerBase extends Controller
{

	/**
	 * 跳转
	 */
	protected  function redirect($uri ='',$data=array())
	{
		if(strpos($uri, '://') === false){
			$uri = $this->url->get($uri,$data);
		}
		ob_clean();
		header('Location:'.$uri);
		exit;
	}

	/**
	 * 发送js消息
	 * @param  $msg
	 * @param  $url
	 */
	protected  function  senJsMsg($msg,$url ='')
	{
		ob_clean();
		if(empty($url)) $url = $this->url->get('');

		header('Content-type:text/html;charset=utf-8');
		$msg = addslashes($msg);
		echo '<script type="text/javascript">alert("'.$msg.'");window.location="'.$url.'";</script>';
		exit;
	}

	/**
	 * 發送json數據
	 * @param  $data
	 * arrary = (status=1, msg='', data='', dialog='')
	 */
	protected  function sendJson($data)
	{
		if (is_array($data) || is_object($data)){
			$data = JSON::encode($data);
		}
		header('Content-Type:application/json; charset=utf-8');
		header('Content-Length:' . strlen($data));
		echo $data;
		exit();
	}


}